package com.example.ui.contact.db;

import java.util.Collections;
import java.util.LinkedList;

import com.example.ui.contact.bean.BaseItem;
import com.example.ui.contact.bean.MeItem;
import com.example.ui.contact.bean.NoticeItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class NoticeMsgDB {
	public static final String MSG_DBNAME = "Msg.db";
	private static final String NoticeMsg_TABLE_NAME = "noticeMsg";
	private SQLiteDatabase db;
	@SuppressWarnings("static-access")
	public NoticeMsgDB(Context context){
		db = context.openOrCreateDatabase(MSG_DBNAME,Context.MODE_APPEND,null);
		db.execSQL("CREATE table IF NOT EXISTS "
				+ NoticeMsg_TABLE_NAME
				+ " (NoticeMsg_TABLE_NAME INTEGER PRIMARY KEY AUTOINCREMENT,content TEXT,dateLine TEXT)");

		
	}

	public void saveNoticeMsg(NoticeItem item){

		
		db.execSQL(
				"insert into "
						+ NoticeMsg_TABLE_NAME
						+ " (content,dateLine) "
						+"values(?,?)",
				new Object[] { item.getContent(), item.getDateLine()});
	
	}

	public LinkedList<NoticeItem> getNoticeMsgList() {
		LinkedList<NoticeItem> list = new LinkedList<NoticeItem>();
		Cursor c = db.rawQuery("SELECT * from " + NoticeMsg_TABLE_NAME, null);
		while (c.moveToNext()) {
			String content= c.getString(c.getColumnIndex("content"));
			String dateLine = c.getString(c.getColumnIndex("dateLine"));


			NoticeItem item = new NoticeItem();
			item.setContent(content);
			item.setDateLine(dateLine);		
			list.add(item);
		}
		//Collections.sort(list);// 按时间降序
		return list;
	}
	
	public void close() {
		if (db != null)
			db.close();
	}
	
	
	private boolean isExist(String userId) {
		Cursor c = db.rawQuery("SELECT * FROM " + MSG_DBNAME
				+ " WHERE userId = ?", new String[] { userId });
		return c.moveToFirst();
	}	
	
}
